<?
function drizzo_move_to_warehouse($id = null, $edited_time = null) {
	global $drizzo_warehouse;
	
	if($edited_time == null) {
		$edited_time = time();
	}
	
	$warehouse_post = drizzo_get_the_post($id);
	extract($warehouse_post);
	$editor = session_user_id;
	$warehouse = mysql_query("INSERT INTO `$drizzo_warehouse` SET `author`='$author',`type`='$type',`status`='$status',`page_parent`='$page_parent',`page_order`='$page_order',`title`='$title',`content`='$content',`tags`='$tags',`comment_status`='$comment_status',`edited_by`='$editor',`edited_date`='$edited_time',`posted_date`='$posted_date_raw',`permalink_name`='$permalink_name',`menu_name`='$menu_name',`lat`='$lat',`lon`='$lon',`old_id`='$ID'");
	
	if(!mysql_errno()) {
		$warehouse_limit = drizzo_global_options("warehouse_limit");
		$old_warehouse_query = @mysql_query("SELECT `ID` FROM `$drizzo_warehouse` WHERE `old_id`='$id' ORDER BY `ID` DESC");
		if(!mysql_errno()) {
			while($row = @mysql_fetch_assoc($old_warehouse_query)) {
				if($count >= $warehouse_limit) {
					@mysql_query("DELETE FROM `$drizzo_warehouse` WHERE `ID`='$row[ID]' LIMIT 1");
				}
				$count++;
			}
		}
		return true;
	} else {
		return false;
	}
}

function drizzo_warehouse_restore($id) {
	global $drizzo_posts;
	$warehouse_post = drizzo_warehouse_revision($id);
	extract($warehouse_post);
	$editor = session_user_id;
	$restore = @mysql_query("UPDATE `$drizzo_posts` SET `title`='$title',`content`='$content',`tags`='$tags',`posted_date`='$posted_date_raw',`author`='$author',`type`='$type',`status`='$status',`comment_status`='$comment_status',`page_parent`='$page_parent',`menu_name`='$menu_name',`page_order`='$page_order',`permalink_name`='$permalink_name',`edited_date`='" . time() . "',`edited_by`='$editor' WHERE `ID`='$old_id'");
	if(!mysql_errno()) {
		return "<p class=\"save\">The Warehouse Revision has been restored.</p>";
	} else {
		return "<p class=\"alert\">The Warehouse Revision could not be restored.</p>";
	}
}

function drizzo_warehouse_revision($id) {
	global $drizzo_warehouse;
	
	$warehouse_query = @mysql_query("SELECT * FROM `$drizzo_warehouse` WHERE `ID`='$id' LIMIT 1");
	if(!mysql_errno()) {
		$revision = drizzo_db_unwrap(@mysql_fetch_assoc($warehouse_query));
		$author_meta = drizzo_user_info($revision['author']);
		if($author_meta['nice_name']) {
			$author = $author_meta['nice_name'];
		} else {
			$author = $author_meta['username'];
		}
		$editor_meta = drizzo_user_info($revision['edited_by']);
		if($editor_meta['nice_name']) {
			$editor = $editor_meta['nice_name'];
		} else {
			$editor = $editor_meta['username'];
		}
		if(drizzo_global_options('time_format')) {
			$time = drizzo_global_options('time_format');
		} else {
			$time = "M d, Y \\a\\t g:i A";
		}
		$revision['author_nicename'] = $author;
		$revision['editor_nicename'] = $editor;
		$revision['posted_date'] = date($time, $revision['posted_date']);
		$revision['posted_date_raw'] = $revision['posted_date'];
		$revision['edited_date'] = date($time, $revision['edited_date']);
		$revision['edited_date_raw'] = $revision['edited_date'];
		return $revision;
	} else {
		return false;
	}
}

function drizzo_warehouse_revisions($id) {
	global $drizzo_warehouse;
	
	$warehouse_query = @mysql_query("SELECT * FROM `$drizzo_warehouse` WHERE `old_id`='$id' ORDER BY `ID` DESC");
	if(!mysql_errno()) {
		while($row = @mysql_fetch_assoc($warehouse_query)) {
			$author_meta = drizzo_user_info($row['author']);
			if($author_meta['nice_name']) {
				$author = $author_meta['nice_name'];
			} else {
				$author = $author_meta['username'];
			}
			$editor_meta = drizzo_user_info($row['edited_by']);
			if($editor_meta['nice_name']) {
				$editor = $editor_meta['nice_name'];
			} else {
				$editor = $editor_meta['username'];
			}
			if(drizzo_global_options('time_format')) {
				$time = drizzo_global_options('time_format');
			} else {
				$time = "M d, Y \\a\\t g:i A";
			}
			foreach($row as $field => $value) {
				if($field != 'posted_date' && $field != 'edited_date') {
					$warehouse_posts[$i][$field] = drizzo_db_unwrap($value);
				}
			}
			$warehouse_posts[$i]['author_nicename'] = $author;
			$warehouse_posts[$i]['editor_nicename'] = $editor;
			$warehouse_posts[$i]['posted_date'] = date($time, $row['posted_date']);
			$warehouse_posts[$i]['posted_date_raw'] = $row['posted_date'];
			$warehouse_posts[$i]['edited_date'] = date($time, $row['edited_date']);
			$warehouse_posts[$i]['edited_date_raw'] = $row['edited_date'];
			$i++;
		}
		return $warehouse_posts;
	} else {
		return false;
	}
}

function drizzo_warehouse_revisions_list($id = null, $list_type = "ul", $list_item_before = "<li>", $list_item_after = "</li>") {
	global $the_post;

	if($id == null && !$the_post) {
		return false;
	} elseif($id == null && isset($the_post)) {
		$id = $the_post['ID'];
	}
	
	$warehouse_posts = drizzo_warehouse_revisions($id);
	if($warehouse_posts) {
		$warehouse_list = "<$list_type id=\"warehouse_list\">\n";
		foreach($warehouse_posts as $warehouse_post) {
			$warehouse_list .= "\t" . $list_item_before . "<a href=\"warehouse.php?id=$warehouse_post[ID]\" title=\"View this Revision\">" . date('M j, y - g:i:s A', $warehouse_post['edited_date_raw']) . " by " . $warehouse_post['editor_nicename'] . "</a>" . $list_item_after . "\n";
		}
		$warehouse_list .= "</$list_type>\n";
		return $warehouse_list;
	} else {
		return false;
	}
}

function drizzo_warehouse_compare($warehouse_content = null, $post_content = null) {
	if($warehouse_content == $post_content) { // If the revisions are identical
		$markup['post'] = $post_content;
		$markup['warehouse'] = $warehouse_content;
	} else { // If the revisions are different, markup the differences;
		preg_match_all("'(.*)'m",$warehouse_content,$warehouse_lines);
		preg_match_all("'(.*)'m",$post_content,$post_lines);
		$warehouse_lines = $warehouse_lines[0];
		$post_lines = $post_lines[0];
		foreach($post_lines as $line) {
			if(in_array($line,$warehouse_lines)) {
				$post_markup .= $line;
			} else {
				$post_markup .= "<span class=\"rev_add\">$line</span>";
			}
		}
		foreach($warehouse_lines as $line) {
			if(in_array($line,$post_lines)) {
				$warehouse_markup .= $line;
			} else {
				$warehouse_markup .= "<span class=\"rev_del\">$line</span>";
			}
		}
		$post_markup = preg_replace('^\<span class="rev_(.){3,6}"\>(.*)(\n)+(.*)\</span\>^', '<span class="rev_$1">$2</span>$3<span class="rev_$1">$4</span>', $post_markup);
		$post_markup = str_replace(array('</span><span class="rev_del">','</span><span class="rev_add">'),'',$post_markup);
		$warehouse_markup = preg_replace('^\<span class="rev_(.){3,6}"\>(.*)(\n)+(.*)\</span\>^', '<span class="rev_$1">$2</span>$3<span class="rev_$1">$4</span>', $warehouse_markup);
		$warehouse_markup = str_replace(array('</span><span class="rev_del">','</span><span class="rev_add">'),'',$warehouse_markup);
		$markup['post'] = $post_markup;
		$markup['warehouse'] = $warehouse_markup;
	}
	return $markup;
}

?>
